package org.example.user.controller;


import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.constant.MessageConstant;
import org.example.dto.LoginDto;
import org.example.result.Result;
import org.example.user.service.UserService;
import org.example.utils.JwtUtils;
import org.example.vo.LoginVo;
import org.example.vo.UserInfoVo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.example.user.config.JwtProperties;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fanfutian
 * @since 2025-04-24
 */
@Slf4j
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private final JwtProperties jwtProperties;

    private final UserService userService;

    private final StringRedisTemplate stringRedisTemplate;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }


    /**
     * 登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        String username = loginDto.getUsername();
        String password = loginDto.getPassword();
        log.info("用户名："+username+" 密码："+password);
        LoginVo loginVo = userService.login(username,password);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", loginVo.getId());
        String token = JwtUtils.createJWT(
                jwtProperties.getUserSecretKey(), // 秘钥，配置文件中读取
                jwtProperties.getUserTtl(),       // 有效时间，配置文件中读取
                claims);

        LoginVo LoginVO = LoginVo.builder()
                .id(loginVo.getId())
                .username(loginVo.getUsername())
                .token(token)
                .build();

        log.info("登录成功，生成的token：{}",  token);

        // 将token保存到redis中
        stringRedisTemplate.opsForValue().set(MessageConstant.TOKEN, token,5, TimeUnit.MINUTES);
        return Result.success(LoginVO);
    }




    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public Result logout(){
        // 退出登录，删除redis中的token
        stringRedisTemplate.opsForValue().getOperations().delete(MessageConstant.TOKEN);
        return Result.success();
    }


    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    public Result<UserInfoVo> getUserInfo(){
        log.info("查询用户信息");
        UserInfoVo userInfoVo = userService.getUserInfo();
        return Result.success(userInfoVo);
    }


    /**
     * 修改用户信息
     * @param userInfoVo
     * @return
     */
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfoVo userInfoVo){
        log.info("批量修改用户信息");
        userService.updateUserInfo(userInfoVo);
        return Result.success();
    }



    /**
     * 修改用户手机号
     * @param phone
     * @return
     */
    @PostMapping("/updateUserPhone/{phone}")
    public Result updateUserPhone(@PathVariable String phone){
        log.info("修改用户手机号");
        userService.updateUserPhone(phone);
        return Result.success();
    }

}
